package rise.net;

import rise.comunity.UserData;
import rise.message.Login;
import rise.message.Packet;
import rise.message.Registration;
import rise.ui.R;
import android.app.Activity;
import android.content.Context;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class RegistrationTask extends SenderTask {

	private final Activity activity;
	private ProgressBar progressBar;
	private Button loginButton, registrationButton;
	private String username, password;
	// private boolean checked;
	private int port;
	private double lat, lon;

	public RegistrationTask(Activity activity,
			rise.message.Registration registration, Context context) {
		super(registration, context);
		this.activity = activity;
		progressBar = (ProgressBar) activity
				.findViewById(R.id.accessProgressBar);
		loginButton = (Button) activity.findViewById(R.id.accessLogin);
		registrationButton = (Button) activity
				.findViewById(R.id.accessRegistration);
		username = registration.getUsername();
		password = registration.getPassword();
		port = registration.getPort();
		lat = registration.getLat();
		lon = registration.getLon();
		// checked =
		// ((CheckBox)activity.findViewById(R.id.registrationRicordati)).isChecked();
	}

	@Override
	protected void onPreExecute() {
		progressBar.setVisibility(ProgressBar.VISIBLE);
		loginButton.setClickable(false);
		registrationButton.setClickable(false);
	}

	@Override
	protected Integer doInBackground(Void... params) {
		return super.doInBackground(params);
	}

	@Override
	protected void onPostExecute(Integer result) {
		this.hideProgressBar();
		if (result == Packet.CONNECTION_ERROR
				|| result == Packet.UNKNOWN_PACKET) {
			super.onPostExecute(result);
		}
		if (result == Registration.INVALID_USERNAME) {
			this.invalidUsername();
			super.onPostExecute(result);
			exitFromApplication();
		}
		if (result == Packet.OK) {
			this.registrationCompleted();
			super.createSharedPreferences(username, password,
					rise.comunity.Status.ONLINE);

			/*UserData.createUserData(username, password,
					rise.comunity.Status.ONLINE);
			UserData.obtainUserData().setSocket(socket/*, printWriter, bufferedReader);*/
			Login login = new Login(username, password, port, lat, lon);
			new LoginTask(activity, login, getContext(), true)
					.execute(new Void[0]);
			return;
		} else {
			loginButton.setClickable(true);
			registrationButton.setClickable(true);
		}
	}

	private void invalidUsername() {
		Toast toast = Toast.makeText(getContext(), "Username già esistente.",
				Toast.LENGTH_LONG);
		toast.show();
	}

	private void registrationCompleted() {
		Toast toast = Toast
				.makeText(
						getContext(),
						"Registrazione effettuata. Il sistema stà predisponendo il tuo dispositivo.",
						Toast.LENGTH_LONG);
		toast.show();

	}

	private void hideProgressBar() {
		progressBar.setVisibility(ProgressBar.INVISIBLE);
		loginButton.setClickable(false);
		registrationButton.setClickable(false);
	}
}